package cn.puge.mertisfront.controller;


import cn.puge.commonutils.R;
import cn.puge.mertisfront.entity.PugeDuly;
import cn.puge.mertisfront.entity.PugeLike;
import cn.puge.mertisfront.service.PugeDulyService;
import cn.puge.mertisfront.service.PugeLikeService;
import cn.puge.servicebase.exceptionhandler.PuGeiException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * 责任书
 * @author LIJW
 * @since 2020-09-18
 */
@CrossOrigin
@RestController
@RequestMapping("/mertisfront/puge-duly")
public class PugeDulyController {

    @Autowired
    private PugeDulyService pugeDulyService;


    /**
     * 根据当前用户id 查询自己的责任书
     * @param id
     * @return
     */
    @GetMapping("getUserIdDuly/{id}")
    public R getUserIdDuly(@PathVariable String id){

        QueryWrapper<PugeDuly> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id);
        queryWrapper.orderByDesc("gmt_create");

        List<PugeDuly> pugeDulies = pugeDulyService.list(queryWrapper);

        return R.ok().data("pugeDulies",pugeDulies);
    }


    /**
     * 添加责任书
     * @param pugeDuly
     * @return
     */
    @PostMapping("addDuly")
    public R addDuly(@RequestBody PugeDuly pugeDuly){
        if (StringUtils.isEmpty(pugeDuly)){
            throw new PuGeiException(20001,"添加不能为空");
        }
        pugeDulyService.save(pugeDuly);
        return R.ok();
    }

}

